package com.haierp.service.authority.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.authority.OrganizationMapperHai;
import com.haierp.model.authority.OrganizationHai;
import com.haierp.service.authority.IOrganizationServiceHai;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.authority.OrganizationQueryVO;

/**
 * 
 * Title: OrganizationServiceHaiImpl.java
 * Description: 
 *
 * @author jc
 * Apr 23, 2017
 *
 */
@Service("organizationServiceHai")
public class OrganizationServiceHaiImpl extends SuperServiceImpl<OrganizationMapperHai, OrganizationHai> implements IOrganizationServiceHai {

	@Autowired
    private OrganizationMapperHai iOrganizationMapperHai;
    
    /**
     * 
     * @param code
     * @return true: exist; false: no exist
     */
    @Override
    public Boolean checkOrganizationNameIsExist(String name, Long id) {
    	OrganizationHai organization = new OrganizationHai();
		organization.setName(name);;
		EntityWrapper<OrganizationHai> entityWrapper = new EntityWrapper<OrganizationHai>(organization);
		if (null != id) {
			entityWrapper.where("id != {0}", id);
		}
		Integer count = iOrganizationMapperHai.selectCountByEw(entityWrapper);
		
		return new Boolean(count != null && count > 0);
	}

    @Override
    public Integer queryOrganizationsCount(OrganizationQueryVO organizationQueryVO) {
		return iOrganizationMapperHai.queryOrganizationsCount(organizationQueryVO);
	}
    
	@Override
	public JsonPageResult<List<OrganizationHai>> queryOrganizationList(OrganizationQueryVO organizationQueryVO) {
		JsonPageResult<List<OrganizationHai>> result = new JsonPageResult<>();
		
		Integer totalCount = iOrganizationMapperHai.queryOrganizationsCount(organizationQueryVO);
		
		if ((null != totalCount) && (0L != totalCount)) {
			result.buildPage(totalCount, organizationQueryVO);
			
			List<OrganizationHai> list = iOrganizationMapperHai.queryOrganizationQueryList(organizationQueryVO);
			result.setData(list);			
		} else {
			List<OrganizationHai> list = new ArrayList<>();
			result.setData(list);
		}
		
		return result;
	}
}